package com.pig4cloud.pig.hu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.common.core.exception.CheckedException;
import com.pig4cloud.pig.hu.entity.Customer;
import com.pig4cloud.pig.hu.mapper.CustomerMapper;
import com.pig4cloud.pig.hu.service.CustomerService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper,Customer> implements CustomerService {
	public boolean anyExists(LambdaQueryWrapper<Customer> wrapper) {
		return baseMapper.selectCount(wrapper) > 0;
	}

	public void checkUnique(Customer customer) {
		if (anyExists(Wrappers.<Customer>lambdaQuery()
				.eq(Customer::getCustomerName, customer.getCustomerName())
				.ne(customer.getCustomerId() != null, Customer::getCustomerId, customer.getCustomerId()))) {
			throw new CheckedException("客户已存在");
		}
		if (anyExists(Wrappers.<Customer>lambdaQuery()
				.eq(Customer::getPhone, customer.getPhone())
				.ne(customer.getCustomerId() != null, Customer::getCustomerId, customer.getCustomerId()))) {
			throw new CheckedException("手机号重复");
		}
		if (anyExists(Wrappers.<Customer>lambdaQuery()
				.eq(Customer::getIdentityCard, customer.getIdentityCard())
				.ne(customer.getCustomerId() != null, Customer::getCustomerId, customer.getCustomerId()))) {
			throw new CheckedException("身份证号重复");
		}
	}
}
